package fm.dongman.modellib.source;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.util.HashMap;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.CommentEditContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.model.CommentModel;

/**
 * 发送评论
 * Created by KUIGE on 2017/11/22.
 */

public class CommentEditRepository implements CommentEditContract.ICommentEditDataSource {

    private static CommentEditRepository SINGLETON;
    private boolean isRemote = true;

    public static CommentEditRepository getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new CommentEditRepository();
        }
        return SINGLETON;
    }

    private CommentEditRepository() {

    }

    @Override
    public void submitComment(@NotNull IModel.ICommentModel comment, LoadSourceCallback<? super String> callback) {
        if (isRemote) {
            HashMap<String, String> map = new HashMap<>();
            CommentModel model = (CommentModel) comment;
            map.put("receive_user_id", model.getTarget());
            map.put("content", model.getContent());
            map.put("target_id", model.getSubjectId());
            map.put("target_type", model.getType());
            map.put("session_key", StaticUrl.TOKEN_KEY);
            JSONObject jSONObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.SEND_COMMENT, map);
            if (jSONObject == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(null);
        } else callback.onDataLoadFiled();
    }

    @Override
    public void getData(LoadSourceCallback<? super String> callback) {

    }

    @Override
    public void saveData(String dataModel) {

    }

    @Override
    public void refreshData() {

    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
